

import { loadEnv } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import path from 'path';
import { resolve } from 'path';
// import viteCompression from 'vite-plugin-compression';
import uniReadPagesV3Plugin from './src/mall/router/utils/uni-read-pages-v3.js';
import mpliveMainfestPlugin from './src/mall/libs/mplive-manifest-plugin';
import AutoImport from 'unplugin-auto-import/vite'

// https://vitejs.dev/config/
export default (command, mode) => {
  const env = loadEnv(mode, __dirname, 'VITE_');
  return {
    envPrefix: "VITE_",
    base: mode === 'production' ? './' : '/',
    plugins: [
      uni(),
      AutoImport({
        imports: [
          'vue',
          'pinia',
        ],
        dirs: ['./src/mall/store'],

        dts: './types/auto-imports.d.ts'
      }),
      uniReadPagesV3Plugin({
        pagesJsonDir: path.resolve(__dirname, './src/pages.json'),
        includes: ['path', 'aliasPath', 'name', 'meta'],
      }),
      mpliveMainfestPlugin(env.VITE_MPLIVE_ON)
    ],
    css: {
      preprocessorOptions: {
        scss: {
          charset: false,
          additionalData: `@import "./src/uni.scss";`
        },
      },
    },
    resolve: {
      alias: {
        '~': resolve(__dirname, './src/mall'),
        '@': resolve(__dirname, './src'),
        'components': resolve(__dirname, './src/components'),
        'api': resolve(__dirname, './src/api'),
      },
      extensions: ['.js', '.json', '.ts', '.vue'], // 使用路径别名时想要省略的后缀名，可以自己 增减
    },
    server: {
      host: true,
      // open: true,
      port: 8002,
      hmr: {
        overlay: true,
      },


    },
  };
};
